﻿      using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.WebControls;
using Eis51.CampanyCMS.Business.Bus;
using Eis51.CampanyCMS.Model.Bus;
using Eis51.CampanyCMS.Business;
using Eis51.CampanyCMS.Model.NewsManager;
using Eis51.CampanyCMS.Model.NewsManager.QueryO;

namespace Eis51.CampanyCMS.UI.Admin.SystemManager
{
    public partial class UserMenuPage : System.Web.UI.Page
    {
        public int Licd
        {
            get
            {
                if (Request.QueryString["Licd"] != null)
                {
                    return int.Parse(Request.QueryString["Licd"]);
                }
                return 2052;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                //权限判断                  
                //if (!ChectRight(AdminType.Admin))
                //{
                //    GoNoRightPage();
                //}
                

                DataTable dt = UsermenuBLL.GetInstance().GetUserNamelist();
                dUserList.DataSource = dt;
                dUserList.DataTextField = "CreateBy";
                dUserList.DataValueField = "CreateBy";
                dUserList.DataBind();
            }
        }

        private void InitData()
        {
            RadTree1.Nodes.Clear();

            DataTable dt = UsermenuBLL.GetInstance().GetDataByUserName(txtUsername.Text);
            dt.DataSet.Relations.Add("NodeRelation", dt.DataSet.Tables[0].Columns["Seqno"], dt.DataSet.Tables[0].Columns["ParentId"], false);

            foreach (DataRow dbRow in dt.DataSet.Tables[0].Rows)
            {

                if (dbRow["ParentId"].ToString() == "0")//这个就是第一层。
                {
                    RadTreeNode node = CreateNode(dbRow["Title"].ToString(), true, dbRow["Seqno"].ToString());
                    node.Category = "Folder";
                    RadTree1.AddNode(node);
                    RecursivelyPopulate(dbRow, node);
                }
            }

            dt.DataSet.Relations.Remove("NodeRelation");
        }

        

        private void RecursivelyPopulate(DataRow dbRow, RadTreeNode node)
        {
            foreach (DataRow childRow in dbRow.GetChildRows("NodeRelation"))
            {
                RadTreeNode childNode = CreateNode(childRow["Title"].ToString(), true, childRow["Seqno"].ToString());
                node.AddNode(childNode);
                RecursivelyPopulate(childRow, childNode);
            }
        }


        private RadTreeNode CreateNode(string text, bool expanded, string id)
        {
            RadTreeNode node = new RadTreeNode(text);
            node.Expanded = expanded;
            node.ID = id;
            return node;
        }



        protected void RadTree1_NodeEdit1(object o, RadTreeNodeEventArgs e)
        {
            RadTreeNode nodeEdited = e.NodeEdited;
            string newText = e.NewText;
            if (newText == null || newText == string.Empty)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "RadTree1_NodeEdit1", "<script>alert('命名不能为空！');</script>");
            }
            else
            {
                Usermenu p = UsermenuBLL.GetInstance().Get(new Usermenu.Key(int.Parse(nodeEdited.ID)));
                p.Title  = txtTitle.Text ;
                p.Navigateurl = txtNavigateUrl.Text ;
                p.Show = chkShow.Checked?1:0;
                p.Isdefault = chkIsDefault.Checked ? 1 : 0;
                p.Id =txtid.Text==string.Empty ?"0": txtid.Text;
                p.Createby = txtUsername.Text;
                p.Parentid = int.Parse(dlist.SelectedValue);
                UsermenuBLL.GetInstance().Update(p);
                GenerateTreeView();
            }
        }

        protected void RadMenu1_ItemClick1(object sender, RadMenuEventArgs e)
        {
            HandleItemClicked(e);
        }

        private void HandleItemClicked(RadMenuEventArgs e)
        {
            switch (e.Item.Value)
            {
                case "Delete":
                    if (RadTree1.SelectedNode.Nodes.Count > 0)
                    {


                        Page.ClientScript.RegisterStartupScript(this.GetType(), "Delete", "<script>alert('还存在有子节点，请先删除子节点！');</script>");

                    }
                    else
                    {
                        if (RadTree1.SelectedNode == RadTree1.Nodes[0]
                            && RadTree1.Nodes.Count == 1)
                        {
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "Delete1", "<script>alert('类型树必须要有一个以上节点，请不要删除！');</script>");
                        }
                        else
                        {

                            UsermenuBLL.GetInstance().Delete(UsermenuBLL.GetInstance().Get(new Usermenu.Key(int.Parse(RadTree1.SelectedNode.ID))));
                            GenerateTreeView();
                        }
                    }
                    break;
                case "AddSubLevel":
                    RadTreeNode rt = new RadTreeNode("新类型");
                    Usermenu p2 = new Usermenu();
                    p2.Title = "新类型";
                    p2.Parentid = int.Parse(RadTree1.SelectedNode.ID);
                    p2.Navigateurl = "";
                    p2.Show = 0;
                    p2.Id = "0";
                    p2.Createby = txtUsername.Text;
                    p2.Createdate = DateTime.Now;
                    p2.Id = "0";
                    p2.Isdefault = 0;
                    UsermenuBLL.GetInstance().Insert(p2);
                    RadTree1.SelectedNode.AddNode(rt);
                    GenerateTreeView();
                    break;
                case "AddSameLevel":
                    RadTreeNode rt1 = new RadTreeNode("新类型");
                    Usermenu p3 = new Usermenu();
                    p3.Title = "新类型";
                    p3.Parentid = RadTree1.SelectedNode.Parent == null ? 0 : int.Parse(RadTree1.SelectedNode.Parent.ID);
                    p3.Navigateurl = "";
                    p3.Show = 0;
                    p3.Id = "0";
                    p3.Createby = txtUsername.Text;
                    p3.Createdate = DateTime.Now;
                    p3.Isdefault = 0;
                    UsermenuBLL.GetInstance().Insert(p3);
                    RadTree1.SelectedNode.AddNode(rt1);

                    GenerateTreeView();
                    break;
            }
            ReNewCache();
        }

        private void GenerateTreeView()
        {
            RadTree1.Nodes.Clear();

            DataTable dt = UsermenuBLL.GetInstance().GetDataByUserName(txtUsername.Text );

            dt.DataSet.Relations.Add("NodeRelation", dt.DataSet.Tables[0].Columns["Seqno"], dt.DataSet.Tables[0].Columns["ParentId"], false);

            foreach (DataRow dbRow in dt.DataSet.Tables[0].Rows)
            {

                if (dbRow["ParentId"].ToString() == "0")//这个就是第一层。
                {
                    RadTreeNode node = CreateNode(dbRow["Title"].ToString(), true, dbRow["Seqno"].ToString());
                    node.Category = "Folder";                    
                    RadTree1.AddNode(node);
                    RecursivelyPopulate(dbRow, node);
                }
            }
            dt.DataSet.Relations.Remove("NodeRelation");
        }

        private void ReNewCache()
        {

           // Dict.CacheData.ClearLocalCache(CacheName.AreaType.ToString());
        }




        protected void RadMenu2_ItemClick1(object sender, RadMenuEventArgs e)
        {
            HandleItemClicked(e);

        }

        protected void RadTree1_NodeClick(object o, RadTreeNodeEventArgs e)
        {
            SerId.Value = RadTree1.SelectedNode.ID;
            SetValue();
           
        }

        private void SetValue()
        {

            Usermenu p = new Usermenu();
            if (SerId.Value != string.Empty)
            {
                p = UsermenuBLL.GetInstance().Get(new Usermenu.Key(int.Parse(SerId.Value)));
            }
            txtid.Text = p.Id.ToString();
            txtNavigateUrl.Text = p.Navigateurl;
            txtTitle.Text = p.Title;
            chkIsDefault.Checked = p.Isdefault == 1 ? true : false;
            chkShow.Checked=p.Show==1?true :false ;
            dlist.SelectedValue = p.Parentid.ToString();
            this.btnSave.Enabled = true;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            if (SerId.Value != string.Empty)
            {
                Usermenu p = UsermenuBLL.GetInstance().Get(new Usermenu.Key(int.Parse(SerId.Value)));

                p.Title = txtTitle.Text;
                p.Navigateurl = txtNavigateUrl.Text;
                p.Show = chkShow.Checked ? 1 : 0;
                p.Isdefault = chkIsDefault.Checked ? 1 : 0;
                p.Id = txtid.Text == string.Empty ? "0" : txtid.Text;
                p.Createby = txtUsername.Text;
                p.Parentid = int.Parse(dlist.SelectedValue);
                UsermenuBLL.GetInstance().Update(p);
                ClaerFace();
                InitData();
             

            }
        }

        private void ClaerFace()
        {
            SerId.Value = "";
            txtTitle.Text = "";
            txtNavigateUrl.Text = "";
            chkShow.Checked = false;
            chkIsDefault.Checked = false;
            txtid.Text = "";
        }

        protected void bntSearch_Click(object sender, EventArgs e)
        {
            dlist.Items.Clear();
            UsermenuBLL.GetInstance().BindDropListTreeType(dlist,txtUsername.Text );
            dlist.Items.Insert(0, new ListItem("请选择", "0"));

            InitData();
        }

        protected void bntcopy_Click(object sender, EventArgs e)
        {
            if (chkDelete.Checked)
            {
                UsermenuBLL.GetInstance().DeleteMyMenu(txtUsername.Text);
            }

            UsermenuBLL.GetInstance().CopyMenu(dUserList.SelectedValue, txtUsername.Text);
        }

        protected void bntCreate_Click(object sender, EventArgs e)
        {
            NewsTypeQuery query = new NewsTypeQuery();
            query.CreateBy = txtUsername.Text ;
            query.Licd = this.Licd ;
            UsermenuBLL.GetInstance().SaveMenu(query);

        }


      
    }
}
